package com.sduept.nwld.wave.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.manager.FaultManager;
import com.sduept.nwld.wave.manager.ReportManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 故障简报REST服务
 * @author Ningfw
 * @date 2017-06-23
 *
 */
@RestController
@RequestMapping("/report/")
public class ReportServices {

	@Autowired
	private ReportManager manager;
	@Autowired
	private FaultManager fm;
	
	
	// 根据故障时间和线路id,获取录波文件id
		/**
		 * 获取故障录播文件信息,如果找到双端返回2条数据,如果找不到返回长度为0的二维数组
		 * 
		 * @param faultTime
		 * @param lineId
		 * @return [[faultFileId,录波器Id,录波器名称,stationId,stationName]]
		 */

		@GetMapping("/getFaultFileId/{faultTime}/{lineId}")
		public String getLineFaultFileId(@PathVariable("faultTime") String faultTime,
				@PathVariable("lineId") String lineId) {
			String[][] str = null;
			// JSONArray rtn = new JSONArray();
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dateTime = sdf.parse(faultTime);
				str = fm.findFaultFileId(lineId, dateTime);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			return JSONObject.toJSONString(str);
		}
	
	/*
	 * 获取故障报告(从t_lbtz_fault_report里获取)
	 * @param faultTime
	 * @param lineId
	 * @return report
	 * */

	@GetMapping("/getReport/{faultTime}/{lineId}")
	public String getReport(@PathVariable("faultTime") String faultTime,@PathVariable("lineId") String lineId){
		String[][] str = null;
		List<JSONObject> list=new ArrayList<>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dateTime = sdf.parse(faultTime);
			str = fm.findFaultFileId(lineId, dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for(int i=0;i<str.length;i++){
			JSONObject jso=new JSONObject();
			JSONObject result=manager.getReport(str[i][0]);
			if(result!=null){
				jso.put("name",str[i][4]);
				jso.put("value", result);
				list.add(jso);
			}
		}
		return JSONArray.toJSONString(list);
	}
	
}
